define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teacher_job/index' + location.search,
                    edit_url: 'teacher_job/edit',
                    del_url: 'teacher_job/del',
                    multi_url: 'teacher_job/multi',
                    import_url: 'teacher_job/import',
                    table: 'teacher_job',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                showToggle: false,
                exportTypes : ['excel'],
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search : false,
                commonSearch : false,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'status', title: __('Status'),searchList: {"-2":__('Status -2'),"-1":__('Status -1'),"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4')},formatter: Table.api.formatter.status},
                        {field: 'user.nickname',operate: 'LIKE', title: __('User_id'), width: '200',formatter:function(v,row){
                                var html = '<div style="display:flex"><img width="60px" height="60px" src="'+Fast.api.cdnurl(row.user.avatar)+'" /><p style="text-align:left;line-height:20px;margin-top:5px;margin-left:10px">ID:'+row.user.id+'<br/>'+row.user.nickname+'<br/>'+row.user.mobile+'</p></div>';
                                return html;
                            }
                        },
                        {field: 'accept_cut', title: '接单截止时间'},
                        {field: 'give_timesec', title: __('Give_timesec'), operate: 'LIKE', searchList: {"half_hour":__('Half_hour'),"a_hour":__('A_hour'),"a_day":__('A_day'),"three_day":__('Three_day'),"a_week":__('A_week')}, formatter: Table.api.formatter.status},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'student', title: __('Student'), operate: 'LIKE'},
                        {field: 'sex', title: __('Sex'), operate: 'LIKE'},
                        {field: 'grade', title: __('Grade'), operate: 'LIKE'},
                        {field: 'subject', title: __('Subject')},
                        {field: 'state', title: __('State'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'learn_date', title: __('Learn_date'), operate: 'LIKE', formatter:function(v,row){
                            if(row.can_discuss==1){
                                return row.learn_date + ' ' + row.learn_time + '<br/>可协商';
                            }else{
                                return row.learn_date + ' ' + row.learn_time;
                            }
                        }},
                        {field: 'teacher_sex', title: __('Teacher_sex'), operate: 'LIKE'},
                        {field: 'teacher_type', title: __('Teacher_type'), operate: 'LIKE'},
                        // {field: 'demand', title: __('Demand'), operate: 'LIKE'},
                        {field: 'normal', title: __('Normal'), searchList: {"0":__('Normal 0'),"1":__('Normal 1')}, formatter: Table.api.formatter.status},
                        {field: 'refundtime', title: __('Refundtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons : [
                                {
                                    name: 'apply',
                                    text: '报名',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-list',
                                    visible: function (row){
                                        if(row.has_err){
                                            return false;
                                        }else{
                                            return true;
                                        }
                                    },
                                    url: function (row) {
                                        return `staff_order/index?job_id=${row.id}`;
                                    }
                                },
                                {
                                    name: 'apply',
                                    text: '报名',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-warning',
                                    visible: function (row){
                                        if(row.has_err){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    url: function (row) {
                                        return `staff_order/index?job_id=${row.id}`;
                                    }
                                },
                                {
                                    name: 'feedback',
                                    text: '意见反馈',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-warning',
                                    visible: function (row){
                                        if(row.has_feedback){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    url: function (row) {
                                        return `feedback/index?job_order_id=${row.id}`;
                                    }
                                },
                                {
                                    name: 'refund',
                                    text: __('退家长押金'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    confirm: '退家长押金并不影响订单状态，请知晓。',
                                    url: 'teacher_job/refund',
                                    visible:(row) =>{
                                        if(!row.refundtime && row.status!==0 && row.status!==-1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    refresh: true
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
